<?php 
class Advert extends CS_Controller
{
    private $advertArray;
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('advert_model', 'advert');
        $this->advertArray = array(
            '1'=>'首页幻灯片广告',
            '2'=>'景区首页幻灯片格广告',
            '3'=>'酒店首页幻灯片广告',
            '4'=>'线路首页幻灯片广告',
            '5'=>'景区特价活动广告',
            '6'=>'数据登录首页幻灯片',
            '7'=>'供应商登录首页幻灯片',
            '8'=>'手机wap端首页头部广告位',
            '9'=>'手机wap端首页底部广告位',
            '10'=>'手机wap端线路首页广告位',
            '11'=>'登录页幻灯片广告',
            '12'=>'商品首页幻灯片广告',
            '13'=>'手机wap端商品首页广告位',
            '14'=>'手机wap端景区首页广告位',
            '15'=>'手机wap端商城天天特惠',
            '16'=>'手机wap端周边游头部广告位',
            '17'=>'移动端商城店铺首页幻灯图',
            '18'=>'手机wap端就近送首页幻灯图',
            '19'=>'手机wap端酒店首页广告位',
            '20'=>'杭州联合银行周周特惠',
        );
    }
    
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('advert/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('advert/grid');
        $config['total_rows'] = $this->advert->total($this->input->get());
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['line_list'] = $this->advert->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['advertArray'] = $this->advertArray;
        $this->load->view('advert/grid', $data);
    }
    
    public function toggle()
    {
        $id = $this->input->post('id');
        $status = $this->input->post('status');
        switch ($status) {
            case '1': $flag = '0'; break;
            case '0': $flag = '1'; break;
            default : $flag = '0'; break;
        }
        $this->db->trans_start();
        $isUpdate = $this->advert->updateStatus(array('id' => $id, 'status' => $flag));
        $this->db->trans_complete();
    
        if ($this->db->trans_status() === TRUE && $isUpdate) {
            echo json_encode(array(
                'status' => $flag,
            ));
        } else {
            echo json_encode(array(
                'status' => 3,
            ));
        }
        exit;
    }
    
    public function add()
    {
        $data['advertArray'] = $this->advertArray;
        $this->load->view('advert/add', $data);
    }
    
    public function addPost()
    {
        $error = $this->validate();
        if (!empty($error)) {
            $this->error('advert/add', '', $error);
        }

        $this->db->trans_start();
        $resultId = $this->advert->insertIntoAdvert($this->input->post());
        $this->db->trans_complete();
        
        if ($resultId) {
            $this->success('advert/grid', '', '保存成功！');
        } else {
            $this->error('advert/add', '', '保存失败！');
        }
    }
    
    public function edit($id)
    {
        $result = $this->advert->findById($id);
        if($result->num_rows() <= 0) {
            $this->redirect('advert/grid');
        }
        $data['advert'] = $result->row();
        $data['advertArray'] = $this->advertArray;
        $this->load->view('advert/edit', $data);
    }
    
    public function editPost()
    {
        $advert_id = $this->input->post('advert_id');
        $error = $this->validate();
    
        if (!empty($error)) {
            $this->error('advert/edit', $advert_id, $error);
        }
        $data['update_time'] = date('Y-m-d H:i:s');
        $this->db->trans_start();
        $resultId = $this->advert->updateInfoAdvert($this->input->post());
        $this->db->trans_complete();
    
        if ($resultId) {
            $this->success('advert/grid', '', '保存成功！');
        } else {
            $this->error('advert/edit', $advert_id, '保存失败！');
        }
    }
    
    public function delete($id)
    {
        $is_delete = $this->advert->deleteById($id);
        
        if ($is_delete) {
            $this->success('advert/grid', '', '删除成功！');
        } else {
            $this->error('advert/grid', '', '删除失败！');
        }
    }

    public function validate()
    {
        $error = array();
        if ($this->validateParam($this->input->post('position'))) {
            $error[] = '展示位置不能为空';
        }
        if ($this->validateParam($this->input->post('title'))) {
            $error[] = '标题不能为空';
        }
        if ($this->validateParam($this->input->post('openvar'))) {
            $error[] = '打开参数不能为空';
        }
        return $error;
    }
}